/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package se.kth.lwr.jeat.utils;

import org.jscience.physics.amount.Amount;
import se.kth.lwr.jeat.core.*;

/**
 * It is often suitable to describe activities ({@link Activity}) as a product
 * of an {@link InventoryDataSource} and an arbitrary Amount. This class takes
 * as argument a {@code DataSource<Inventory>} (the multiplicand) and a {@code DataSource<Amount>}
 * (the multiplier). The multiplication of these sources are "injected" into the
 * activity and result of the operation is summed up to the corresponding Inventory.
 *
 * @author davidev
 */
public class MultiplierActivity extends Activity {

    private DataSource<Inventoriable> multiplicand;
    private DataSource<Amount> multiplier;

    /**
     * Creates a new MultiplierActivity
     *
     * @param name name of activity
     * @param description description of activity
     * @param productSource the base InventoryDataSource for this product (the
     * multiplicand)
     * @param multiplier the factor to multiply the source with
     */
    public MultiplierActivity(String name, String description, DataSource<Inventoriable> productSource, DataSource<Amount> multiplier) {
        super(name, description);
        if (productSource == null) {
            throw new NullPointerException("InventoryDataSource must be a non-null value");
        }
        if (multiplier == null) {
            throw new NullPointerException("MultiplyerArgument must be a non-null value");
        }
        multiplicand = productSource;
        this.multiplier = multiplier;
    }

    @Override
    public Inventory contribute(Inventory inventory) {
        Inventory contrib = multiplicand.useValue(this).getInventory();
        contrib = contrib.multiply(multiplier.useValue(this));
        return inventory.merge(contrib);
    }
}
